package com.example.foodisimportant.RegisterActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.foodisimportant.LoginActivity.LoginTest;
import com.example.foodisimportant.Network.NetworkSettings;
import com.example.foodisimportant.Network.LoginNetworkThread;
import com.example.foodisimportant.Network.RegisterNetworkThread;
import com.example.foodisimportant.R;
import com.example.foodisimportant.Body.ResponseBody;
import com.example.foodisimportant.StudentViewActivity.StudentViewActivity;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.concurrent.FutureTask;

public class RegisterTest extends AppCompatActivity {

    //定义
    private EditText mETName,mETNumber,mETPwd,mETConfirmPwd;
    private TextView mBtnJumptologin;
    private Button mBtnSummit;
    private RadioGroup mRgcampus;
    private RadioButton mRbcampus;
    private String id,name,pwd,confirmpwd,campus,code;

    private final ObjectMapper mapper = new ObjectMapper();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //对应布局
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        //对应控件
        mBtnSummit=findViewById(R.id.btn_summit);
        mETNumber=findViewById(R.id.et_id);
        mETName=findViewById(R.id.et_name);
        mETPwd=findViewById(R.id.et_pwd);
        mETConfirmPwd=findViewById(R.id.et_confirmpwd);
        mRgcampus=findViewById(R.id.rdgroup_position);
        mRbcampus=findViewById(R.id.rbg1);
        mBtnJumptologin=findViewById(R.id.tv_jumptologin);
        mRgcampus.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                mRbcampus=findViewById(group.getCheckedRadioButtonId());
            }
        });

        mBtnSummit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        id=mETNumber.getText().toString();
                        name = mETName.getText().toString();
                        pwd = mETPwd.getText().toString();
                        confirmpwd = mETConfirmPwd.getText().toString();
                        campus = mRbcampus.getText().toString();
                        //判断输入是否符合标准,不符合则弹窗使之重新输入
                        if(name.length() == 0){
                            Toast.makeText(RegisterTest.this,"用户名不能为空",Toast.LENGTH_SHORT).show();
                        }
                        else if(4 >= name.length()){
                            Toast.makeText(RegisterTest.this,"用户名长度应在4-18字之间",Toast.LENGTH_SHORT).show();
                        }
                        else if(name.length() >= 18){
                            Toast.makeText(RegisterTest.this,"用户名长度应在4-18字之间",Toast.LENGTH_SHORT).show();
                        }
                        else if(name.length() >= 18){
                            Toast.makeText(RegisterTest.this,"用户名长度应在4-18字之间",Toast.LENGTH_SHORT).show();
                        }
                        //这里比较两个密码是否相同，不能用== 或者!=来判断string，string是引用类型的
                        //不是基本数据类型，所以它们比较使用地址和值（相当于C的指针）
                        //来比较的，所以 str == str2永远是false，那么就应该将Text取出比较
                        //getText得到内容
                        //toString以str形式
                        //trim可以删去前后的空格
                        //如果两部分相等则等于1，注册成功，关掉页面回到登陆页面
                        else if(pwd.equals(confirmpwd)){
                            FutureTask<String> signUpTask = new FutureTask<>(new RegisterNetworkThread(id,name,pwd,campus,NetworkSettings.SIGN_UP));
                            Thread thread = new Thread(signUpTask);
                            thread.start();
                            try{
                                ResponseBody body = mapper.readValue(signUpTask.get(), ResponseBody.class);
                                if(body.getCode() == 1){
                                    Toast.makeText(getApplicationContext(),body.getMsg()+"注册成功",Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(RegisterTest.this, StudentViewActivity.class);
                                    finish();
                                    startActivity(intent);
                                }else {
                                    Toast.makeText(getApplicationContext(),"注册失败",Toast.LENGTH_SHORT).show();
                                    Intent intent=new Intent(RegisterTest.this, LoginTest.class);
                                    finish();
                                    startActivity(intent);
                                }
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        else {
                            Toast.makeText(RegisterTest.this,"两次输入的密码不一致",Toast.LENGTH_SHORT).show();
                        }

                    }
                }).start();
            }
        });
        mBtnJumptologin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent= new Intent(RegisterTest.this, LoginTest.class);//跳转到登录界面
                startActivity(intent);
            }
        });
    }



}
